package au.com.bluelink.scjp.chap8;

public class AnonymousInnerClassTest {

	public static void main(String[] args) {
		// Anonymous Inner Classes, Flavor One (anonymous subclass)
		Food f = new Food();
		f.p.pop();

		// Anonymous Inner Classes, Flavor Two (anonymous implementer)
		Food2 f2 = new Food2();
		f2.c.cook();

		// Argument-Defined Anonymous Inner Classes
		MyWonderfulClass mwc = new MyWonderfulClass();
		mwc.go();
		
		// Static Nested Classes
		Broom.test();
		
	}

}

class Popcorn {
	public void pop() {
		System.out.println("popcorn");
	}
}

class Food {
	Popcorn p = new Popcorn() {
		public void pop() {
			System.out.println("anonymous popcorn (subclass of Popcorn)");
			super.pop();
		}
	};
}

interface Cookable {
	public void cook();
}

class Food2 {
	Cookable c = new Cookable() {
		public void cook() {
			System.out.println("anonymous cookable implementer");
		}
	};
}

class MyWonderfulClass {
	void go() {
		Bar b = new Bar();
		b.doStuff(new Foo() {
			public void foof() {
				System.out.println("foofy");
			} // end foof method
		}); // end inner class def, arg, and b.doStuff stmt.
	} // end go()
} // end class

interface Foo {
	void foof();
}

class Bar {
	void doStuff(Foo f) {
		f.foof();
	}
}

class BigOuter {
	static class Nest {
		void go() {
			System.out.println("hi");
		}
	}
}

class Broom {
	static class B2 {
		void goB2() {
			System.out.println("hi 2");
		}
	}

	public static void test() {
		BigOuter.Nest n = new BigOuter.Nest(); // both class names
		n.go();
		B2 b2 = new B2(); // access the enclosed class
		b2.goB2();
	}
}